/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin.archiver.targz;

import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;

import com.google.code.debmavenplugin.archiver.AbstractCollectingFileFilter;
import com.google.code.debmavenplugin.archiver.ArchiveMetaData;
import com.google.code.debmavenplugin.archiver.ArchiveMetaData.ArchiveEntryMetaData;


/**
 * File filter to collect &quot;tar&quot; archive entries.
 */
public class TarArchiveCollectionFileFilter extends AbstractCollectingFileFilter<FileTarArchiveEntry> {

  private Set<FileTarArchiveEntry> archiveContent;

  /**
   * Constructor.
   *
   * @param metaData The archive's metadata.
   * @param useRelativePaths <code>true</code> to create relative paths for the
   * archive entries, <code>false</code> else.
   */
  public TarArchiveCollectionFileFilter(ArchiveMetaData metaData, boolean useRelativePaths) {
    super(metaData, useRelativePaths);
    this.archiveContent = new TreeSet<FileTarArchiveEntry>(new Comparator<TarArchiveEntry>() {

      @Override
      public int compare(TarArchiveEntry o1, TarArchiveEntry o2) {
        return o1.getFile().compareTo(o2.getFile());
      }
    });
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Set<FileTarArchiveEntry> getCollectedFiles() {
    return Collections.unmodifiableSet(this.archiveContent);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected void collectFile(File file) {
    FileTarArchiveEntry archiveEntry = new FileTarArchiveEntry(file, createPath(getMetaData().getBaseDirectory(), file));
    setAttributes(archiveEntry, getMetaData().getEntryMetaData(file));
    this.archiveContent.add(archiveEntry);
  }

  /**
   * Helper method to set the attributes of the archive entry.
   *
   * @param archiveEntry The archive entry.
   * @param entryMetaData Metadata defining the attributes of the archive entry.
   */
  private void setAttributes(TarArchiveEntry archiveEntry, ArchiveEntryMetaData entryMetaData) {
    if (entryMetaData.getUserName() != null) { archiveEntry.setUserName(entryMetaData.getUserName()); }
    if (entryMetaData.getGroupName() != null) { archiveEntry.setGroupName(entryMetaData.getGroupName()); }
    if (entryMetaData.getFileMode() != null) { archiveEntry.setMode(entryMetaData.getFileMode()); }
  }
}
